from django.db import models
from datetime import datetime
from mlh.utils.models import BaseModel
from django.contrib import auth
# Create your models here.


class News(BaseModel):
    """新闻"""

    title = models.CharField(max_length=64, verbose_name='新闻标题')  # 新闻标题
    user = models.ForeignKey('users.User', on_delete=models.CASCADE, related_name='newsset', verbose_name='用户')  # 发布新闻用户
    content = models.TextField(verbose_name='新闻内容')  # 新闻内容
    digest = models.TextField(max_length=128, verbose_name='新闻摘要')  # 新闻摘要
    category = models.ForeignKey('Category', on_delete=models.CASCADE, related_name='categoryset', verbose_name='新闻分类')  # 新闻分类
    # image = models.ImageField(upload_to='news_image', verbose_name='新闻图片')  # 新闻图片
    status = models.BooleanField( default=0, verbose_name='新闻审核状态')   # 新闻审核状态, 默认未审核
    # reason = models.CharField(max_length=128, verbose_name='审核未通过原因', )   # 当新闻审核状态为-1时用
    is_collected = models.BooleanField(default=0, verbose_name='新闻是否收藏')  # 新闻是否收藏
    comments = models.IntegerField(verbose_name='评论条数',default=0)  # 评论条数
    class Meta:
        db_table = 'tb_news'
        verbose_name = '新闻详情'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Category(BaseModel):
    """新闻分类"""

    name = models.CharField(max_length=32, verbose_name='新闻分类')  # 新闻分类名

    class Meta:
        db_table = 'tb_category'
        verbose_name = '新闻分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Comment(BaseModel):
    """评论"""

    user = models.ForeignKey('users.User', on_delete=models.CASCADE, related_name='usersset', verbose_name='用户')  # 评论用户
    news = models.ForeignKey('News', on_delete=models.CASCADE, related_name='newsset', verbose_name='新闻')  # 评论新闻
    content = models.TextField(verbose_name='评论内容')   # 评论内容
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True, blank=True, verbose_name='评论父评论')
    count = models.IntegerField(verbose_name='回复条数')  # 回复条数

    class Meta:
        db_table = 'tb_comment'
        verbose_name = '评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "评论%d" % self.id

